/*!
 * 数值输入控件.
 * ecp.qzz.ui.input.number.js
 * 创建：QZZ
 * 日期：2014-03-01
 */
define(["qzz.iedit"], function() {
	
	var qzz = window.qzz;
	qzz.space("ui.input");
	/**
	 * 数值输入控件.
	 */
	qzz.ui.input.number = qzz.Class(qzz.ui.input.edit, {
		/**
		 * 创建.
		 */
	    _create:function(){
		    this.base();
		    this.className = "Number";
		    this.logInfo("number.create");
		    //精度
            this.dataScale = 0;
            this.size = this._getNumberValue(this.getOption().size || this.getOption().length, 15);
            this.numUnit = 1;
            this._maxValue = null;
            this._minValue = null;
		    if(this.getOption().maxValue != null) {
		    	this._maxValue = parseInt(this.getOption().maxValue, 10);
		    }
		    if(this.getOption().minValue != null) {
		    	this._minValue = parseInt(this.getOption().minValue, 10);
		    }
	    },
	    /**
	     * 渲染.
	     */
	    _render:function(){
	    	this.base();
	    	this.logInfo("number.render");
	    	this.inputBox.onkeydown = this.doInput;
	    	this.inputBox.onkeyup = this.doInputerKeyUp;
	    },
	    setMinValue:function(mv) {
	    	this._minValue = mv;
	    },
	    setMaxValue:function(mv) {
	    	this._maxValue = mv;
	    },
	    /**
	     * 设置精度
	     */
	    setDataScale:function(ds){
            this.dataScale = ds;
        },
        /**
         * 设置格式化
         */
        setFormatStr:function(fs){
            this.format = fs;
        },
        /**
         * 设置单元单位值.
         */
        setNumUnit:function(nu) {
        	this.numUnit = nu;
        },
        isNumberKey:function(key) {
	        //48-57：小键盘区的数字、96-105：主键盘区的数字
	        return key >= 96 && key <= 105 || key >= 48 && key <= 57;
        },
        /**
         * 数值键限制.
         * @param key 键值
         */
        numberLimitKey:function(key) {
            if(!(this.isNumberKey(key) || key == 110 || key == 109 || key == 189 || key == 190 || key == 229)
                ||key == 111 || key == 106 || key == 107 ) {
                return false;
            }
            return true;
        },
        /**
         * 提交值.
         */
        postValue:function() {
        	var v = this.inputBox.value;
        	if(isNaN(v)) {
        		v = "";
        	}
        	if(v === "" && this.notnull === true) return;
        	if(!this.isEmpty(v)) {
        		if(v === "9999999999999999") {
        			v = 9999999999999999;
        		} else {
        		    v = parseFloat(v, 10);
        		}
        	}
        	this.setValue(v);
        }, 
        /**
         * 获取值.
         * @return 返回
         */
        getValue:function() {
        	var v = this.base();
        	if(v !== "") {
	        	if(this.isEmpty(v)) {
	        		v = 0;
	        	} else if(typeof v == "string"){
	        		v = parseFloat(v, 10);
	        	}
        	}
        	return v;
        },
        /**
         * 设置值.
         * @param value 值
         */
        setValue:function(value) {
        	 if(typeof value == "object") {
        		 //如果是json对象则拆分
        		 if(value[this.getOption().idkey] != this._value 
        				 || value[this.getOption().idkey] === "" && this._value === 0
        				     || value[this.getOption().idkey] === 0 && this._value === "") {
	        		 this._value = value[this.getOption().idkey];
	        		 if(value[this.getOption().capkey] != null) {
	        			 var text = value[this.getOption().capkey] + "";
		        		 this.inputBox.value = text.replace(/[,%‰]/g,"");
		        		 this._text = this.inputBox.value;
		        		 if(this.numUnit == 1 && value[this.getOption().idkey] != null 
		        				 && value[this.getOption().idkey] !== "" 
		        					 && value[this.getOption().idkey] != this.inputBox.value) {
		        			 this.inputBox.value = value[this.getOption().idkey];
		        			 this._text = value[this.getOption().idkey];
		        		 }
	        		 } else {
	        			 if(value[this.getOption().idkey] !== "") {
			        		 this.inputBox.value = this._value/this.numUnit;
			        		 this._text = this.inputBox.value;
	        			 } else {
	        				 this.inputBox.value = "";
	        				 this._text = "";
	        			 }
	        		 }
	        		 this._isChanged = true;
	        		 this.refresh();
	        		 //变更事件.
	        		 if(typeof this.onChanged == "function" && this._update) {
	        			 this.onChanged(this._value, this._text);
	        		 }
        		 }
        	} else if(this._value != value 
        		|| this.numUnit != 1 && this.numUnit != null
        			|| value === "" && this._value === 0
				      || value === 0 && this._value === "") {
        		 if(value !== "") {
	        		 //如果和原来值不一样则更新
	        		 this._value = value * this.numUnit;
	        		 this.inputBox.value = value;        		 
	        		 this._text = this.inputBox.value;
	        		 this._isChanged = true;
	        		 this.refresh();	        		 
        		 } else {
        			 this._value = "";
        			 this._text = "";
        			 this.inputBox.value = "";
        		 }
        		//变更事件.
        		 if(typeof this.onChanged == "function" && this._update) {
        			 this.onChanged(this._value, this._text);
        		 }
        	}         	 
        	if(this.numUnit != null) {
        		//重新计算一下单位.
        		this.numUnit = parseFloat(this.numUnit, 10);
        		this._text = this._value / this.numUnit;
        		this.inputBox.value = this._text;
        		this.refresh();
        	}
        },
        /**
         * 获取输入后的数据.
         */
        _getAfterValue:function(oldValue, index, key, tx) {
        	if((key == 187 || key == 107 || key == 189 || key == 109) && index == 0 && oldValue.indexOf("-") < 0) {
        		return "-" + oldValue;
        	}
        	if(key >= 48 && key <= 57) {
        		key -= 48;
        	} else if(key >= 96 && key <= 105){
        		key -= 96;
        	} else {
        		return oldValue;
        	}
        	var ov = oldValue + "";
        	var rv = "";
        	if(tx == null || tx === "") {
	        	var fv = ov.substring(0, index);
	        	var lv = ov.substring(index, ov.lenvth);
	        	rv = fv + key + lv;
        	} else {
        		rv = ov.replace(tx, key);
        	}
        	return rv;
        },
        /**
         * 输入控制.
         * @param ev 事件对象
         */
        doInput:function(ev) {
	        var ent = ev || window.event;
            var key = ent.keyCode || ent.charCode || ent.which;
            var _owner = this.ctrl;
            ent.returnValue = true;
            if(ent.ctrlKey && key != 17) {//_ctrlKeyDown) {
            	//控制键, 
    	        if(key != 67 && key != 86 && key != 88 ) {
                    ent.returnValue = false;
                }
    	        //粘贴
    	        if(key == 86) {
    	        	//ent.returnValue = false;
    	        	_owner.paste = true;
    	        }
            } else if(ent.shiftKey && key != 16) {//if(_shiftKeyDown) {
            	//上档键控制
                if(key != 35 && key != 36 && key != 67 && key != 86 && !_owner.isCtrlKey(key)) {
                    ent.returnValue = false;
                }
            } else {
            	//精度初始化
            	if(_owner.size == null) {
            		_owner.size = 15;
            	}
                if(_owner.dataScale == null) {
                	_owner.dataScale = 0;
                }
                if(_owner.dataScale == 0 && key == 110) {
                	ent.returnValue = false;
                	return false;
                }
                //限制输入为16位，
                if(_owner.size > 16) {
                	_owner.size = 16;
                }
    	        var vlen = this.value.length;
    	        var ilen = _owner.size - _owner.dataScale;
    	        //鼠标位置及选择状态
    	        var ci = _owner.getPosition(this);
    	        var pi = this.value.indexOf(".");
    	        var tx = _owner.getSelText(this); 
    	        //输入控制
            	if((_owner._maxValue != null && _owner._maxValue !== "" 
            		|| _owner._minValue != null && _owner._minValue !== "") && key != 8 && key != 46) {
        	    	if(_owner._minValue === 0 && (key == 189 || key == 109)) {
        	    		ent.returnValue = false;
        	    	}
            	    var re = _owner._getAfterValue(this.value, ci, key, tx);
            	    if(_owner._maxValue != null && _owner._maxValue !== "" && re > _owner._maxValue) {
            	    	ent.returnValue = false;
            	    } else if(_owner._minValue != null && _owner._minValue !== "" && re < _owner._minValue) {
            	    	ent.returnValue = false;
            	    }
            	    if(ent.returnValue == false && re != this.value) {
            	        return ent.returnValue;
            	    }
            	}
    	        //计算有没有超出范围
    	        if(key == 109 || key == 189 || key == 173) {
    	        	if(ci > 0 || this.value.indexOf("-") >= 0) {
    	        		ent.returnValue = false;
    	        	}
                } else if (!(key >= 35 && key <= 36) && key != 17 && key != 20 && key != 8 && key != 46 && !_owner.isCtrlKey(key)) {
                	ent.returnValue = _owner.numberLimitKey(key);
                	var iv = this.value.split(".");
                	if(iv.length == 2) {
                		var selCant = tx.length > 0 && tx.indexOf(".") >= 0 && ilen <= (vlen - tx.length);
                		if(selCant) {
                			ent.returnValue = false;
                		} else {
                			if(iv[0].length >= ilen && iv[1].length >= _owner.dataScale) {	
	                			if(tx.length == 0) {	                		
		                		    ent.returnValue = false;
	                			}
	                		} else {
	                			var ib = this.value.indexOf(".");
	                			if(iv[0].length >= ilen && ci <= ib || iv[1].length >= _owner.dataScale && ci > ib) {
	                				if(tx.length == 0) {
	                				    ent.returnValue = false;
	                				}
	                			}
	                			ib = null;
	                		}
		                		
	                	}
                		
                		selCant = null;
                	} else if(vlen >= ilen && key != 110 && key != 190){
                		if(tx === "") {
                		    ent.returnValue = false;
                		}
                	}
                	iv = null;
                } else if(key == 8 || key == 46) { 
                	//小数点控制
                	var selCant = tx.length > 0 && tx.indexOf(".") >= 0 && ilen < (vlen - tx.length);
                	if(selCant) {
                		ent.returnValue = false;
                	} else {
                		if(vlen - 1 > ilen) {
	                		if(pi == ci && key == 46) {
	                			ent.returnValue = false;
	                		} else if(pi + 1 == ci && key == 8) {
	                			ent.returnValue = false;
	                		}
                		}
                	}
                	selCant = null;
                } else if(key == 13 || key == 9) {
                	ent.returnValue = false;
                }
                vlen = null;
                ilen = null;
                tx = null;
            	ci = null;            	
			}//ctrl
            if(ent.returnValue) {
            	_owner._isChanged = true;
            }
            _owner = null;
            return ent.returnValue;
        },
        /**
         * 输入键弹起.
         */
        doInputerKeyUp:function(ev) {
        	var ent = ev || window.event;
            var key = ent.keyCode || ent.charCode || ent.which;
            var _owner = this.ctrl;
            if(_owner.paste && key == _owner.keyBoard.V) {
            	value = this.value;
            	if(value.indexOf(/[\n\t]/) < 0) {
            		value = value.replace(/[,%‰]/g,"");
	            	if(isNaN(value)) {
	            		_owner.tmpValue = value;
	            		this.value = "";
	            	} else {
	            		this.value = value;
	            	}
            	}
            }
            //去除中文
            this.value = this.value.replace(/[^0-9.-]/g, "");//.replace(/[\u4e00-\u9fa5]/g, "");
            _owner.paste = false;
        }
	});
	
	/**
	 * 数值编辑控件.
	 */
	_bindRenderFunc("qzznumber", qzz.ui.input.number);
	return qzz.ui.input.number;
});